//
//  ZH_HomeInformationTableViewCell.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/18.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_HomeInformationTableViewCell.h"
#import "ZH_HomeInformationCollectionViewCell.h"
@interface ZH_HomeInformationTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic,weak)UILabel *redLabel;
@property (nonatomic,weak)UILabel *titleLabel;
///内容
@property(nonatomic, weak) UICollectionView *collectionView;
///数据
@property(nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic,weak)UILabel *timeLabel;
@property (nonatomic,weak)UILabel *numLabel;
@property (nonatomic,weak)UIImageView *lookImage;
@end
@implementation ZH_HomeInformationTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_HomeInformationTableViewCell";
    ZH_HomeInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_HomeInformationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
    self.bottomShortLineImage.hidden = NO;
}
- (void)loadSubviewConstraints{
    [self.redLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(FITSCALE(14));
        make.left.equalTo(self.contentView).offset(FITSCALE(12));
        make.width.mas_equalTo(FITSCALE(64));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.redLabel);
        make.left.equalTo(self.redLabel.mas_right).offset(FITSCALE(5));
        make.right.equalTo(self.contentView).offset(FITSCALE(-12));
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.redLabel.mas_bottom).offset(FITHEIGHT(10));
        make.left.equalTo(self.contentView).offset(FITWITH(11));
        make.right.equalTo(self.contentView).offset(FITWITH(-11));
//        make.bottom.equalTo(self.contentView).offset(FITHEIGHT(-1));
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom).offset(FITSCALE(11));
        make.left.equalTo(self.contentView).offset(FITSCALE(22));
        make.bottom.equalTo(self.contentView).offset(FITSCALE(-12));
    }];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLabel);
        make.right.equalTo(self.contentView).offset(FITSCALE(-22));
    }];
    [self.lookImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.numLabel);
        make.right.equalTo(self.numLabel.mas_left).offset(FITSCALE(-4));
    }];
    
}
///更新 ui
- (void)updateUiForData:(NSDictionary *)dic
{
    if(dic){
        self.titleLabel.text = dic[@"title"];
        self.timeLabel.text = dic[@"create_at"];
        self.numLabel.text = [NSString stringWithFormat:@"(%@)",dic[@"view"]];
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:dic[@"top_images"]];
        
        NSInteger item = self.dataArray.count;
        NSInteger rowNum = (item / 3) + ((item % 3) > 0 ? 1 : 0);
        
        CGFloat collectionHeight = FITSCALE(82) * rowNum + (rowNum * FITHEIGHT(0));
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(collectionHeight);
        }];
        
        [self.collectionView reloadData];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(HEIGHT_TRANSFORMATION(0));
        }];
    }
}
#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    if(self.onClickBlock){
//        self.onClickBlock(self.dataArray[indexPath.row][1]);
//    }
    
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size = CGSizeMake((kMainScreenWidth - FITSCALE(34)) / 3, FITSCALE(82));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return FITHEIGHT(0);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return WIDTH_TRANSFORMATION(0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZH_HomeInformationCollectionViewCell *cell = [ZH_HomeInformationCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
    [cell updateImage:self.dataArray[indexPath.row]];
    return cell;
}



#pragma mark -- getter,setter

///内容
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionView *collectionView = [UICollectionView createCollectionViewWithDelegateTarget:self forScrollDirection:UICollectionViewScrollDirectionVertical];
        //        collectionView.pagingEnabled = YES;
        [self.contentView addSubview:_collectionView = collectionView];
        //注册
        [ZH_HomeInformationCollectionViewCell registerCollectionView:collectionView];
    }
    return _collectionView;
}


///数据
- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}
#pragma mark - getter,setter
- (UILabel *)redLabel{
    if (!_redLabel) {
        UILabel *label = [UILabel createLabelWithText:@"【资讯】" font:kFontSize_28];
        label.textColor = [UIColor colorWithHexString:@"#FF4646"];
        [self.contentView addSubview:_redLabel = label];
    }
    return _redLabel;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [UILabel createLabelWithText:@"资讯标题..." font:kFontSize_28];
        [self.contentView addSubview:_titleLabel = label];
    }
    return _titleLabel;
}
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel *label = [UILabel createLabelWithText:@"1天前" font:kFontSize_26];
        label.textColor = [UIColor colorWithHexString:@"#999999"];
        [self.contentView addSubview:_timeLabel = label];
    }
    return _timeLabel;
}
- (UILabel *)numLabel{
    if (!_numLabel) {
        UILabel *label = [UILabel createLabelWithText:@"(12)" font:kFontSize_26];
        label.textColor = [UIColor colorWithHexString:@"#999999"];
        label.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_numLabel = label];
    }
    return _numLabel;
}
- (UIImageView *)lookImage{
    if (!_lookImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"icon_eyes"];
        [self.contentView addSubview:_lookImage = image];
    }
    return _lookImage;
}

@end
